home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17150 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: prairienet.org!wemccaug
  2. From: wemccaug@prairienet.org (Wendy E. McCaughrin)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: [Q]Normalized Random Nos.
  5. Date: 14 Apr 1996 01:30:18 GMT
  6. Organization: University of Illinois at Urbana
  7. Message-ID: <4kpkfa$7or@vixen.cso.uiuc.edu>
  8. References: <N.040696.101306.34@ix.netcom.com> <4k4gmh$7ol@fountain.mindlink.net>
  9. Reply-To: wemccaug@prairienet.org (Wendy E. McCaughrin)
  10. NNTP-Posting-Host: firefly.prairienet.org
  11.  
  12.  
  13. In a previous article, jhewett@ix.netcom.com (Jerry Hewett) says:
  14.  
  15. >John (jswalby@mindlink.bc.ca) asks:
  16. >
  17. >> Does anybody out there have a C or C++ (msvc++) algorithm to generate
  18.    ##########
  19. >> normalized random numbers (i.e. between 0 and 1) using the rand() function?
  20. >
  21. >Try this out -- it's a modified version of the sample program for rand() that 
  22. >comes with the MSVC online help:
  23. >
  24. >----<snip>----
  25. >
  26. >/* RAND.C: This program seeds the random-number generator
  27. > * with the time, then displays 10 random integers.
  28. > */
  29. >
  30. >#include <stdlib.h>
  31. >#include <stdio.h>
  32. >#include <time.h>
  33. >
  34. >void main( void )
  35. >{
  36. >   float    result;
  37. >   int      i;
  38. >
  39. >   /* Seed the random-number generator with current time so that
  40. >    * the numbers will be different every time we run.
  41. >    */
  42. >   
  43. >   srand( (unsigned)time( NULL ) );
  44. >
  45. >   /* Display 10 numbers. */
  46. >   
  47. >   for(i = 0;i < 10;i++ )
  48. >   {
  49. >       result = (float) rand ();
  50. >       printf ("%f\n",result / RAND_MAX);
  51. >   }
  52. >}
  53.  
  54.  No. The builtin random-number generator generates UNIFORMLY-
  55.  distributed pseudo-random numbers, but the author has asked
  56.  for NORMALLY-distributed ones.  If you have access to Knuth,
  57.  Vol.II, you will find an algorithm in there for obtaining
  58.  normally-distributed randoms from the uniformaly-dstributed
  59.  ones you get from 'rand'.
  60.  
  61.